package com.taojin.shoppinginterest.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.bean.ListDetailInfo;
import com.taojin.shoppinginterest.utils.BitmapHelper;

import java.util.List;

public class ListDetailFragmentAdapter extends  AppAdapter<ListDetailInfo> {

    private final int TYPE_1=0;
    private final int TYPE_2=1;
    private BitmapUtils bitmapUtils;
    public ListDetailFragmentAdapter(List<ListDetailInfo> list, Context context) {
        super(list, context);
        bitmapUtils= BitmapHelper.getInstance().getBitmapUtils(context, R.mipmap.ic_launcher,R.mipmap.ic_launcher);
    }

    @Override
    public View createItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 viewHolder1=null;
        ViewHolder2 viewHolder2=null;
        int type=getItemViewType(position);
        if(convertView==null){
            switch (type){
                case TYPE_1:
                    convertView=inflater.inflate(R.layout.item_listfragment_details,parent,false);
                    viewHolder1=new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                    break;
                case TYPE_2:
                    convertView=inflater.inflate(R.layout.item_listfragment_details2,parent,false);
                    viewHolder2=new ViewHolder2(convertView);
                    convertView.setTag(viewHolder2);
                    break;
            }
        }else{
            switch (type){
                case TYPE_1:
                    viewHolder1= (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_2:
                    viewHolder2= (ViewHolder2) convertView.getTag();
                    break;
            }
        }

        switch (type){
            case TYPE_1:
                viewHolder1.tvAddress.setText(list.get(position).getName());
                viewHolder1.tvCate.setText(list.get(position).getCate());
                bitmapUtils.display(viewHolder1.imagNum1, list.get(position).getLogo());
                String[] imageLists=list.get(position).getImg_list();
                bitmapUtils.display(viewHolder1.imageView1,imageLists[0]);
                bitmapUtils.display(viewHolder1.imageView1,imageLists[1]);
                bitmapUtils.display(viewHolder1.imageView1, imageLists[2]);
                String[] addressLists=list.get(position).getTag_list();
                viewHolder1.textName1.setText(addressLists[0]);
                viewHolder1.textName2.setText(addressLists[1]);
                viewHolder1.textName3.setText(list.get(position).getDistance());
                break;
            case TYPE_2:
                viewHolder2.tvAddress1.setText(list.get(position).getName());
                viewHolder2.tvCate1.setText(list.get(position).getCate());
               bitmapUtils.display(viewHolder2.imagNum11, list.get(position).getLogo());
                String[] imageLists2=list.get(position).getImg_list();
                bitmapUtils.display(viewHolder2.imageView11,imageLists2[0]);
                bitmapUtils.display(viewHolder2.imageView21,imageLists2[1]);
                bitmapUtils.display(viewHolder2.imageView31,imageLists2[2]);
                String[] addressLists2=list.get(position).getTag_list();
                viewHolder2.textName11.setText(addressLists2[0]);
                viewHolder2.textName21.setText(addressLists2[1]);
                viewHolder2.textName31.setText(addressLists2[2]);
                viewHolder2.textName41.setText(list.get(position).getDistance());
                break;
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        int size=list.get(position).getTag_list().length;
        switch (size){
            case 2:
                return TYPE_1;
            case 3:
                return TYPE_2;
        }
        return -1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public class ViewHolder1{

        @ViewInject(R.id.imageNum1)
        private ImageView imagNum1;
        @ViewInject(R.id.item_listFragment_detail_address)
        private TextView tvAddress;
        @ViewInject(R.id.item_listFragment_detail_introduce)
        private TextView tvCate;
        @ViewInject(R.id.item_listFragment_detail_image1)
        private ImageView imageView1;
        @ViewInject(R.id.item_listFragment_detail_image2)
        private ImageView imageView2;
        @ViewInject(R.id.item_listFragment_detail_image3)
        private ImageView imageView3;
        @ViewInject(R.id.item_listFragment_detail_textName1)
        private TextView textName1;
        @ViewInject(R.id.item_listFragment_detail_textName2)
        private TextView textName2;
        @ViewInject(R.id.item_listFragment_detail_textName3)
        private TextView textName3;

        public ViewHolder1(View convertView){
            ViewUtils.inject(this,convertView);
        }

    }

    public class ViewHolder2{
        @ViewInject(R.id.imageNum11)
        private ImageView imagNum11;
        @ViewInject(R.id.item_listFragment_detail_address1)
        private TextView tvAddress1;
        @ViewInject(R.id.item_listFragment_detail_introduce1)
        private TextView tvCate1;
        @ViewInject(R.id.item_listFragment_detail_image11)
        private ImageView imageView11;
        @ViewInject(R.id.item_listFragment_detail_image21)
        private ImageView imageView21;
        @ViewInject(R.id.item_listFragment_detail_image21)
        private ImageView imageView31;
        @ViewInject(R.id.item_listFragment_detail_textName11)
        private TextView textName11;
        @ViewInject(R.id.item_listFragment_detail_textName21)
        private TextView textName21;
        @ViewInject(R.id.item_listFragment_detail_textName31)
        private TextView textName31;
        @ViewInject(R.id.item_listFragment_detail_textName41)
        private TextView textName41;

        public ViewHolder2(View convertView){
            ViewUtils.inject(this,convertView);
        }
    }
}
